package com.fjc.utils;

import com.alibaba.fastjson.JSONObject;
import sun.net.www.protocol.http.HttpURLConnection;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Random;

/**
 *
 * UrlConnection Util
 *
 * */
public class UrlConnectUtil {
    /*
     * get HTTP
     * */
    public static String getHttp(String url)throws Exception{
        URL requestURI=new URL(url);
        HttpURLConnection conn=(HttpURLConnection)requestURI.openConnection();//如果是https的请求则是HttpsURLConnection，如果是http则是HttpURLConnection
        conn.setRequestMethod("GET");//设置请求方式
        conn.setDoInput(true);//打开input流
        conn.setRequestProperty("Connection","keep-alive");
        conn.setRequestProperty("charset", "UTF-8");
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type","text/html");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
        InputStream input=null;
        BufferedReader reader=null;
        StringBuffer result=new StringBuffer("");

        input=conn.getInputStream();
        reader=new BufferedReader(new InputStreamReader(input));
        String temp=null;
        while((temp=reader.readLine())!=null){
            result.append(temp);
        }
        reader.close();
        input.close();
        conn.disconnect();
        return result.toString();
    }

    /*
     * get HTTPS
     * */
    public static String getHttps(String url)throws Exception{
        URL requestURI=new URL(url);
        HttpsURLConnection conn=(HttpsURLConnection)requestURI.openConnection();//如果是https的请求则是HttpsURLConnection，如果是http则是HttpURLConnection
        conn.setRequestMethod("GET");//设置请求方式
        conn.setDoInput(true);//打开input流
        conn.setRequestProperty("Connection","keep-alive");
        conn.setRequestProperty("charset", "UTF-8");
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type","text/html");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
        InputStream input=null;
        BufferedReader reader=null;
        StringBuffer result=new StringBuffer("");

        input=conn.getInputStream();
        reader=new BufferedReader(new InputStreamReader(input,"utf-8"));
        String temp=null;
        while((temp=reader.readLine())!=null){
            result.append(temp);
        }
        reader.close();
        input.close();
        conn.disconnect();
        return result.toString();
    }

    /*
     * post HTTP
     * */
    public static String postHttp(String urlstr,String param) throws Exception{
        URL url=new URL(urlstr);
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);//上传参数，需要打开output流，将参数写到流中提交
        conn.setRequestProperty("Connection","keep-alive");
        conn.setRequestProperty("charset", "UTF-8");
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
        StringBuffer params=new StringBuffer();
        //"userName=user123&password=123456"
        params.append(param);
        byte[] bytes=params.toString().getBytes();
        conn.getOutputStream().write(bytes);

        InputStream input=conn.getInputStream();
        BufferedReader reader=new BufferedReader(new InputStreamReader(input));
        String result="";
        String temp="";
        while((temp=reader.readLine())!=null){
            result+=temp;
        }
        reader.close();
        input.close();
        conn.disconnect();
        return result.toString();
    }

    /*
    * post HTTPS
    * */
    public static String postHttps(String urlstr,String param) throws Exception{
        URL url=new URL(urlstr);
        HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);//上传参数，需要打开output流，将参数写到流中提交
        conn.setRequestProperty("Connection","keep-alive");
        conn.setRequestProperty("charset", "UTF-8");
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
        StringBuffer params=new StringBuffer();
        //"userName=user123&password=123456"
        params.append(param);
        byte[] bytes=params.toString().getBytes();
        conn.getOutputStream().write(bytes);

        InputStream input=conn.getInputStream();
        BufferedReader reader=new BufferedReader(new InputStreamReader(input));
        String result="";
        String temp="";
        while((temp=reader.readLine())!=null){
            result+=temp;
        }
        reader.close();
        input.close();
        conn.disconnect();
        return result.toString();
    }

    /*
    * post HTTPS JSON
    * */
    public static String postHttpsJson(String urlstr, JSONObject json) throws Exception{
        URL url=new URL(urlstr);
        HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);//上传参数，需要打开output流，将参数写到流中提交
        conn.setRequestProperty("Connection","keep-alive");
        conn.setRequestProperty("charset", "UTF-8");
        conn.setRequestProperty("Accept", "*/*");
        conn.setRequestProperty("Content-Type","application/json");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
        StringBuffer params=new StringBuffer();
        //"userName=user123&password=123456"
        params.append(json);
        byte[] bytes=params.toString().getBytes();
        conn.getOutputStream().write(bytes);

        InputStream input=conn.getInputStream();
        BufferedReader reader=new BufferedReader(new InputStreamReader(input));
        String result="";
        String temp="";
        while((temp=reader.readLine())!=null){
            result+=temp;
        }
        reader.close();
        input.close();
        conn.disconnect();
        return result.toString();
    }

}
